import { Component, OnInit, Input, ViewChild } from '@angular/core';
import { NzTableComponent } from 'ng-zorro-antd';
import { MESSAGE_CHANNEL, SYSTEM_EVENT } from 'app/app.constants';
import { NotificationService, BullySubjectService } from 'app/shared/service';
import { PublicService } from '../public.service';
import { ConfigService } from 'app/config/config.service';
declare const Print: any;

@Component({
    selector: 'itmp-toolbar',
    templateUrl: './toolbar.component.html',
    styleUrls: ['./toolbar.component.css']
})
export class ToolbarComponent implements OnInit {
    @Input() toolbarList: Array<string>;
    @Input() printDom: any;
    @Input() selectData: any;
    @Input() searchData: any;
    @Input() deviceList = [];
    @Input()
    exportName:
        | 'messageLibrary'
        | 'messageCategory'
        | 'graphic'
        | 'messageTemplate'
        | 'equipmentList'
        | 'equipmentAlarm'
        | 'equipmentAlarmHistory'
        | 'trafficAlert'
        | 'trafficHistoricalAlert'
        | 'IncidentRecordList'
        | 'IncidentRecordHistoryList'
        | 'scheduledEvent'
        | 'histScheduledEvent'
        | 'manageUser'
        | 'manageRole'
        | 'manageAccessRight'
        | 'manageOrganization'
        | 'logManagement'
        | 'ResponsePlan';
    @Input() tableHeader: Array<any>;
    @Input() eventType: SYSTEM_EVENT;
    @Input() accessButton;
    isPhotoVisible = false;
    locatAccess = false; // 定位权限
    photoGroup: any;
    // 请求地址放这里，key值为路由
    exportUrl = {
        messageLibrary: this.config.apiUrl.vms,
        graphic: this.config.apiUrl.vms,
        messageCategory: this.config.apiUrl.vms,
        messageTemplate: this.config.apiUrl.vms,
        equipmentList: this.config.apiUrl.equipment + '/equipMgt/equip/exportDeviceInfo',
        equipmentAlarm: this.config.apiUrl.alarm + '/vnqnalarmmgt/techAlarm/export',
        equipmentAlarmHistory: this.config.apiUrl.alarm + '/vnqnalarmmgt/techAlarm/hist/export',
        trafficAlert: this.config.apiUrl.alert + '/vnqnalertmgt/trafficAlert/export',
        trafficHistoricalAlert: this.config.apiUrl.alert + '/vnqnalertmgt/trafficAlert/hist/export',
        IncidentRecordList: this.config.apiUrl.incident + '/vnqnincidentmgt/exportIRExcel',
        IncidentRecordHistoryList: this.config.apiUrl.incident + '/vnqnincidentmgt/exportHistIRExcel',
        scheduledEvent: this.config.apiUrl.incident + '/vnqnincidentmgt/exportScheduledEventList',
        histScheduledEvent: this.config.apiUrl.incident + '/vnqnincidentmgt/exportHistScheduledEventList',
        manageUser: this.config.apiUrl.user,
        manageRole: this.config.apiUrl.user,
        manageAccessRight: this.config.apiUrl.user,
        manageOrganization: this.config.apiUrl.user,
        logManagement: this.config.apiUrl.user
    };
    constructor(
        private bully: BullySubjectService,
        private msg: NotificationService,
        private pub: PublicService,
        private config: ConfigService
    ) {}

    ngOnInit() {
        const access = JSON.parse(localStorage.getItem('menu')).filter(item => item.accessId === 6);
        this.locatAccess = access.length === 0 ? false : true;
    }

    // 显示表数据
    showTableHeader() {}
    print() {
        let Dom = null;
        if (this.printDom === 'access_right') {
            Dom = $('#access_right')[0];
        } else {
            Dom = $('#nzTable')[0];
        }
        if (Dom) {
            const userAgent = window.navigator.userAgent;
            // IE打印默认字体最小的问题解决
            if (userAgent.indexOf('Edge') !== -1 || (userAgent.indexOf('NET') !== -1 && userAgent.indexOf('rv') !== -1)) {
                document.execCommand('print', false, null);
            }
            Print(Dom, {});
        }
    }
    screnFull() {}
    cctv() {}
    // 导出功能
    download(type) {
        const params = {
            params: {
                condition: this.searchData,
                exportType: type,
                pageNum: 1,
                pageSize: 99999
            },
            url: this.exportUrl[this.exportName]
        };
        const expandedName = type === 'CSV' ? 'xls' : 'pdf';
        this.pub.export(params).subscribe(res => {
            if (res) {
                const blob = new Blob([res]);
                const blobUrl = window.URL.createObjectURL(blob);
                this.downloadFile(blobUrl, this.exportName + '.' + expandedName);
            }
        });
    }
    downloadFile = function(blobUrl, name) {
        const a = document.createElement('a');
        a.style.display = 'none';
        a.download = name;
        a.href = blobUrl;
        a.click();
    };
    positioning() {
        if (!this.selectData) {
            this.msg.notify({ type: 'warning', path: 'Share.Toolbar.select_notify' });
            return;
        }
        if (this.deviceList.length) {
            try {
                this.selectData.cmdList.forEach(item => {
                    const f = this.deviceList.find(dev => dev.eqtId === item.deviceID);
                    item.workStatus = f ? f.workStatus : 'OK';
                });
            } catch (error) {}
        }
        const data = {
            type: this.eventType,
            data: this.selectData
        };
        this.bully.registerBroadcastSend(MESSAGE_CHANNEL.CCM_CHANNEL, data);
    }
    // 打开弹框展示图片
    showPhoto() {
        if (!this.selectData) {
            this.msg.notify({ type: 'warning', path: 'EquipmentManagement.EquipmentManagement.select_notify' });
            return;
        }
        this.photoGroup = this.selectData.url ? this.selectData.url.split(',') : [];
        this.isPhotoVisible = true;
    }
}
